package com.roncoo.education.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.roncoo.education.api.request.MemberRequest;
import com.roncoo.education.db.entity.Member;
import com.roncoo.education.db.service.MemberService;
import com.roncoo.education.util.MyJwtUtil;
import com.roncoo.education.util.UsernameGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Slf4j
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class MemberController {

    @Resource
    private MemberService memberService;

    @Value("${wx.appid}")
    private String wxappId;

    @Value("${wx.secret}")
    private String wxsecret;

    @Resource
    MyJwtUtil myJwtUtil;

    @PostMapping("/editMember")
    public String updateMember(@Valid @RequestBody MemberRequest req,HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = myJwtUtil.getUsernameFromToken(token);
        req.setId(userId);
        Map<String,String> ret = memberService.updateMember(req);
        return JSONUtil.toJsonStr(ret);
    }

    @GetMapping("/getMemeberInfo")
    public String getMember(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = myJwtUtil.getUsernameFromToken(token);
        Member member= memberService.getMemberById(userId);
        return JSONUtil.toJsonStr(member);
    }

    @GetMapping("/weixinLogin")
    public String weiXinLogin(String code){

        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+wxappId+"&secret="+wxsecret+"&js_code="+code+"&grant_type=authorization_code";
        String ret = HttpUtil.get(url);
        String msgCode = "";
        String msg = "";
        if(StrUtil.isNotBlank(ret)){
            JSONObject object = JSONUtil.parseObj(ret);
            String openid = object.getStr("openid");
            msgCode = object.getStr("errcode");
            msg = object.getStr("errmsg");
            if(StrUtil.isNotBlank(openid)){
                MemberRequest request = new MemberRequest();
                request.setOpenId(openid);
                request.setUsername(UsernameGenerator.generate());
                Map<String,String> retMap = memberService.saveMember(request);
                return JSONUtil.toJsonStr(retMap);
            }
        }
        Map<String,String> retMap = new HashMap<>();
        retMap.put("isError", "fail");
        retMap.put("msg", msg);
        retMap.put("msgCode", msgCode);
        retMap.put("code", "998");
        return JSONUtil.toJsonStr(ret);
    }

}
